package com.allwees.bs.c.module.merchant.service.impl;

import com.allwees.bs.c.module.merchant.dto.StoreDTO;
import com.allwees.bs.c.module.merchant.entity.StoreEntity;
import com.allwees.bs.c.module.merchant.mapper.StoreMapper;
import com.allwees.bs.c.module.merchant.repository.StoreRepository;
import com.allwees.bs.c.module.merchant.service.IStoreService;
import com.allwees.bs.c.module.product.dto.ProductDetailsDTO;
import com.allwees.bs.c.module.product.entity.ProductEntity;
import com.allwees.bs.c.module.product.repository.ProductRepository;
import com.allwees.bs.c.module.user.repository.StoreCollectionRepository;
import com.allwees.bs.core.modelbase.constant.ResultEnum;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import com.allwees.bs.core.modelbase.page.PageParams;
import com.allwees.bs.core.modelbase.page.PaginateDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

//import com.allwees.bs.c.module.order.repository.OrderItemRepository;
//import com.allwees.bs.c.module.user.repository.ProductCollectionRepository;

@Service
public class StoreServiceImpl implements IStoreService {

    @Autowired
    private StoreRepository storeRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private StoreMapper storeMapper;

    //@Autowired
    //private ProductCollectionRepository collectionRepository;

    //@Autowired
    //private OrderItemRepository orderItemRepository;

    @Autowired
    private StoreCollectionRepository storeCollectionRepository;

    @Override
    public StoreDTO fetchStore(String userUuid, String storeUuid) {
        StoreEntity storeEntity = this.storeRepository.getByUuid(storeUuid);
        if (storeEntity == null) {
            throw BusinessException.getInstance(ResultEnum.NOT_FOUND);
        }

        StoreDTO storeDTO = this.storeMapper.entity2DTO(storeEntity);

        if (userUuid != null && !"".equals(userUuid)) {
            long count = this.storeCollectionRepository.countByUserAndStoreId(userUuid, storeUuid);
            if (count > 0) {
                storeDTO.setUserIsCollection(true);
            }
        }

        return storeDTO;
    }

    @Override
    public PaginateDTO<ProductDetailsDTO> fetchStoreProducts(String userUuid, String storeUuid, PageParams pageParams) {

        Page<ProductEntity> page = this.productRepository.findStoreProducts(storeUuid, pageParams.toPageable(Sort.Direction.DESC, "createdAt"));
        PaginateDTO<ProductDetailsDTO> products = PaginateDTO.of(page.getTotalElements(), page.getNumberOfElements(), pageParams);

        if (page.getNumberOfElements() == 0) {
            return products;
        }
        List<ProductDetailsDTO> list = page.getContent().stream().map(ProductDetailsDTO::new).collect(Collectors.toList());
        products.setResults(list);
        return products;
    }
}
